package com.crm.manager.service.impl;


import com.crm.manager.domain.Course;
import com.crm.manager.domain.dto.CourseDto;
import com.crm.manager.domain.dto.CoursePageQueryDto;
import com.crm.manager.domain.vo.PageVO;
import com.crm.manager.mapper.CourseMapper;

import com.crm.manager.service.CourseService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;

@Service
@Slf4j
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;


    //添加课程
    @Override
    public void add(CourseDto courseDto) {
        Course course = new Course();
        BeanUtils.copyProperties(courseDto, course);
        course.setCode(add(course));
        course.setCreateTime(LocalDateTime.now());
        courseMapper.add(course);


    }

//    //删除课程
//    @Override
//    public void delete(Long id) {
//        courseMapper.delete(id);
//
//    }


    //分页查询课程
    @Override
    public PageVO pageQuery(CoursePageQueryDto coursePageQueryDto) {
        PageHelper.startPage(coursePageQueryDto.getPageNum(), coursePageQueryDto.getPageSize());
        List<Course> list = courseMapper.pageQuery(coursePageQueryDto);
        Page<Course> p = (Page<Course>) list;
        long total = p.getTotal();
        PageVO pageVO = new PageVO();
        pageVO.setTotal(total);
        pageVO.setRows(p.getResult());
        return pageVO;

    }


    //    查询课程详情
    @Override
    public Course getInfo(Long id) {
        return courseMapper.getInfo(id);
    }


    @Override
    public List<Course> listselect(String subject) {
        log.info("查询课程列表");
        return courseMapper.listselect(subject);
    }

    //修改信息
    @Override
    public void update(Course course) {
        courseMapper.update(course);

    }

    //批量删除
    @Override
    public void deleteBatch(List<Integer> ids) {
        courseMapper.deleteBatch(ids);
    }

    @Override
    public BigDecimal getCoursePriceById(Integer courseId) {
        return courseMapper.getCoursePriceById(courseId);
    }




    public String add(Course course) {
        String code;
        int maxAttempts = 10; // 最多尝试生成 10 次 code
        int attempts = 0;

        do {
            code = CodeGenerator.generateRandomCode(); // 生成随机 code
            attempts++;
            if (attempts > maxAttempts) {
                throw new RuntimeException("无法生成唯一的活动代码，请稍后重试！");
            }
        } while (courseMapper.countByCode(code) > 0); // 检查 code 是否已存在

        course.setCode(code); // 设置唯一的 code
        return code;
    }


    public static class CodeGenerator {

        // 定义字符池：1-9 数字 + 小写字母 + 大写字母
        private static final String CHARACTERS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        private static final int CODE_LENGTH = 8; // code 的长度

        /**
         * 生成随机的 code
         *
         * @return 返回一个随机的 code
         */
        public static String generateRandomCode() {
            Random random = new Random();
            StringBuilder code = new StringBuilder(CODE_LENGTH);
            for (int i = 0; i < CODE_LENGTH; i++) {
                int index = random.nextInt(CHARACTERS.length());
                code.append(CHARACTERS.charAt(index));
            }
            return code.toString();


        }
    }
}

